#include <iostream>
#include <cmath>
using namespace std;

typedef long long ll;

ll compute_sum(ll n, ll b) {
    ll sum = 0;
    while (n > 0) {
        sum += n % b;
        n = n / b;
    }
    return sum;
}

int main() {
    ll n, s;
    cin >> n >> s;

    if (s > n) {
        cout << -1 << endl;
        return 0;
    }

    if (s == n) {
        cout << (n == 1 ? 2 : n + 1) << endl;
        return 0;
    }

    ll min_b = -1;
    ll max_b_candidate = (ll)sqrt(n);

    // 处理情况1：b <= sqrt(n)
    for (ll b = 2; b <= max_b_candidate; ++b) {
        ll sum = compute_sum(n, b);
        if (sum == s) {
            if (min_b == -1 || b < min_b) {
                min_b = b;
            }
        }
    }

    // 处理情况2：b > sqrt(n)
    ll delta = n - s;
    if (delta > 0) {
        ll max_a = min(s, (ll)sqrt(delta));
        for (ll a = 1; a <= max_a; ++a) {
            if (delta % a != 0) continue;
            ll k = delta / a;

            // 处理a的情况
            ll b_candidate = k + 1;
            if (b_candidate > max_b_candidate) {
                ll a_prime = n / b_candidate;
                if (a_prime == a) {
                    ll c = n % b_candidate;
                    if (c == s - a) {
                        if (min_b == -1 || b_candidate < min_b) {
                            min_b = b_candidate;
                        }
                    }
                }
            }

            // 处理k的情况
            if (k == a || k > s) continue;
            ll b_candidate_swap = a + 1;
            if (b_candidate_swap > max_b_candidate) {
                ll a_prime_swap = n / b_candidate_swap;
                if (a_prime_swap == k) {
                    ll c_swap = n % b_candidate_swap;
                    if (c_swap == s - k) {
                        if (min_b == -1 || b_candidate_swap < min_b) {
                            min_b = b_candidate_swap;
                        }
                    }
                }
            }
        }
    }

    cout << min_b << endl;

    return 0;
}